<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class FetchJobData extends Command
{
    protected $signature = 'fetch:jobdata {--examtype=} {--city=} {--year=} {--month=}';
    protected $description = 'Fetch job data from the API and store it in the jobs table';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $client = new Client();
        $baseUrl = 'https://ha.huatu.com/zt/ksrl/api/getArticleList/';
        $page = 1;

        // Redis key for hash set
        $redisKey = 'job_hash_set1';
        $hashSet = Redis::smembers($redisKey);
        $batchSize = 100; // Adjust the batch size as needed
        $dataToInsert = [];

        $examtype = $this->option('examtype');
        $city = $this->option('city');
        $year = $this->option('year');
        $month = $this->option('month','');
        while (true) {
            try {
                $response = $client->request('GET', $baseUrl, [
                    'query' => [
                        'examtype' => $examtype,
                        'city' => $city,
                        'year' => $year,
                        'month' => $month,
                        'page' => $page,
                    ]
                ]);

                $data = json_decode($response->getBody(), true);

                if ($data['code'] !== 200 || empty($data['data'])) {
                    $this->info('No more data available or error occurred.');
                    break;
                }
                $dataToInsert=[];
                foreach ($data['data'] as $item) {
                    $hash = md5($year.$item['id']);

                    // Check if hash is already processed
                    if (in_array($hash, $hashSet)) {
                        continue;
                    }

                    // Add hash to Redis and array
                    Redis::sadd($redisKey, $hash);
                    $hashSet[] = $hash;

                    // Prepare data for batch insert
                    $dataToInsert[] = [
                        'data_id' => $item['id'],
                        'pubdate' => $item['pubdate'],
                        'pub_date' => date('Y-m-d',$item['pubdate']),
                        'title' => $item['title'],
                        'bkggbmsj' => $item['bkggbmsj'],
                        'bkggksnr' => $item['bkggksnr'],
                        'bkggkssj' => $item['bkggkssj'],
                        'bkggzkrs' => (int)$item['bkggzkrs'],
                        'ename' => $item['ename'],
                        'smalltypes' => $item['smalltypes'],
                        'hash' => $hash,
                        'examtype' => $examtype,
                        'city' => $city,
                        'year' => $year,
                        'month' => $month,
                        'page' => $page,
                        'url'=>sprintf('https://ha.huatu.com/%s/%s/%s.html',$year,date('md',$item['pubdate']),$item['id'])
                    ];

                    // Batch insert data if batch size is reached
                    // if (count($dataToInsert) >= $batchSize) {
                    //     DB::table('jobs')->insert($dataToInsert);
                    //     $dataToInsert = []; // Clear the array after insertion
                    // }
                }

                // Insert remaining data if any
                if (!empty($dataToInsert)) {
                    DB::table('jobs')->insert($dataToInsert);
                }
                $this->error('完成第'.$page.'页');
                $this->error('');
                sleep(rand(1,3));
                $page++;
            } catch (RequestException $e) {
                $this->error('page:',$page);
                $page++;
                Log::error('API request failed: ' . $e->getMessage());
                $this->error('Error fetching data: ' . $e->getMessage());
                continue;
            }
        }

        $this->info('Data fetching completed.');
    }
}
